import module namespace functx = "http://www.functx.com" at "WEB-INF/xquery/functx-1.0.xq";
declare variable $doc as node() external;
let $threads := for $thread in $doc//table[@id='threadslist']//td[@class='alt1']//a[starts-with(@id, 'thread_title')]
                    let $href := $thread/@href
                    let $title := $thread/text()
                    
                    (: Isolate thread ID (tXXXXX) from the link :)
                    let $indices := functx:index-of-string($href, '/')
                    let $index := $indices[count($indices) - 1] (: length - 1 is / where tid begins :)
                    let $href := replace(substring($href, $index), '/', '')
                    
                    return
                    <thread id="{$href}">{$title}</thread>

return 
<threads>
    {$threads}
</threads>